package com.deimos.tests;

import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.llom.util.AXIOMUtil;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
import org.vast.ows.sps.SPSUtils;
import org.vast.ows.sps.StatusReport;
import org.vast.ows.sps.UpdateResponse;
import org.vast.ows.sps.StatusReport.RequestStatus;
import org.vast.xml.DOMHelper;

import com.deimos.HMA;
import com.deimos.UtilsDeimos;
import com.deimos.dataBase.TaskController;

public class Update {
    private static HMA hma = new HMA();
    SPSUtils utils = new SPSUtils();
    static String taskId = null;

    @BeforeClass
    public static void init() {
	TaskController taskController = new TaskController();
	taskController.createTables(false);
    }

    @Test
    public void Update_Feasible() throws Exception {
	OMElement request, response;
	request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "Update_Step_1_Submit_Request.xml"));
	response = hma.Submit(request);

	StatusReport report = UtilsDeimos.readParams(response.toString()).getReport();

	if (report.getDescription().contains("in use")) {
	    if (report.getDescription().contains("task:"))
		taskId = report.getDescription().split("task:")[1].split(",")[0].replace(" ", "").replace("Z.", "Z");
	    if (report.getDescription().contains("tasks:"))
		taskId = report.getDescription().split("tasks:")[1].split(",")[0].replace(" ", "").replace("Z.", "Z");
	    cancel_is_in_use();
	    taskId = null;
	    Update_Feasible();
	}

	assertTrue(response.toString().contains("59.70370144832263"));

	if (taskId == null) {
	    taskId = UtilsDeimos.readParams(response.toString()).getReport().getTaskID();
	    assertTrue(UtilsDeimos.readParams(response.toString()).getReport().getRequestStatus() == RequestStatus.Accepted);
	    System.out.println(taskId);
	    Update_Step_2();
	    taskId = UtilsDeimos.readParams(response.toString()).getReport().getTaskID();
	    // Update_Step_3();
	    // taskId =
	    // UtilsDeimos.readParams(response.toString()).getReport().getTaskID();
	    cancel_is_in_use();
	}
    }

    public void Update_Step_2() throws Exception {
	OMElement request, response;
	request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "Update_Step_2_Update_Request.xml").replace("TASKID", taskId));
	response = hma.Update(request);
	
	System.out.println(taskId);
	assertTrue(response.toString().contains("59.56393941951763"));

	if (taskId == null) {
	    taskId = UtilsDeimos.readParams(response.toString()).getReport().getTaskID();
	    InputStream is = new ByteArrayInputStream(response.toString().getBytes());
	    DOMHelper domResponse = new DOMHelper(is, false);
	    StatusReport status = ((UpdateResponse) utils.readXMLResponse(domResponse, domResponse.getRootElement(), "SPS", "UpdateResponse", "2.0")).getReport();
	    assertTrue(status.getRequestStatus() == RequestStatus.Accepted);
	}
    }

    /*
     * public void Update_Step_3() throws Exception{ OMElement request,
     * response; request =
     * AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir +
     * "Update_Step_3_GetStatus_Request.xml").replace("TASKID", taskId));
     * response = hma.GetStatus(request);
     * assertTrue(response.toString().contains("61.67527755932902")); request =
     * AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir +
     * "Update_Step_3_GetStatus_Request.xml").replace("TASKID", taskId));
     * response = hma.GetStatus(request);
     * assertTrue(response.toString().contains("61.67527755932902")); }
     */

    @After
    public void cancel_is_in_use() throws Exception {
	OMElement request;
	if (taskId == null)
	    return;
	request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "Update_Step_4_Cancel_Request.xml").replace("TASKID", taskId));
	hma.Cancel(request);
    }
}
